
cc.Class({
    extends: cc.Component,

    properties: {
        items:[cc.Node]
    },


    onLoad () {
        this.node.zIndex = 3;
        this.pos = new Array();
        for (var i = 0; i < this.items.length; i++) {
            this.pos[i] = this.items[i].getPosition();
        }
        this.init();
    },


    init(){
        this.node.setPosition(cc.v2(0, 0))
        this.flush()
    },


    flush() {
        cc.log("当前关卡：" + data.myLevel)
        var starLevel = data.myLevel - 2;
        for (var i = 0; i < this.items.length; i++) {
            if(this.pos[i] != null ){
                this.items[i].setPosition(this.pos[i]);
            }

            this.items[i].setScale(cc.v2(0.6,0.6));
            this.items[i].opacity = 255;
            var js = this.items[i].getComponent('levelItem');
            js.setLevel(starLevel + i);
        }
        this.items[2].setScale(cc.v2(0.8,0.8));
        this.items[0].opacity = 0;
        this.items[4].opacity = 0;
    },


    moveOut() {
        var moveTo = cc.moveTo(0.5,cc.v2(0,450));
        var scaleTo = cc.scaleTo(0.5,0.4,0.4);
        var spa = cc.spawn(scaleTo,moveTo);
        this.node.runAction(spa.easing(cc.easeBackIn()));
    },



    moveIn:function(isSuc, callBack){
        var moveTo = cc.moveTo(0.5,cc.v2(0,50));
        var scaleTo = cc.scaleTo(0.5,1,1);
        var spa = cc.spawn(scaleTo,moveTo);
        var seq = cc.sequence(spa,  cc.callFunc(()=>{
            if(isSuc){
                this.nextLevel();
                data.addLevel();
            }
            if(callBack!=null) callBack()
        }));
        this.node.runAction(seq.easing(cc.easeBackIn()));

    },    

    nextLevel:function(callBack){
        for (var i = 1; i < this.items.length; i++) {
            var moveTo = cc.moveTo(0.5,cc.v2(this.pos[i-1]));

            if( i == 1){
                var out = cc.fadeOut(0.5);
                var scale = cc.scaleTo(0.5,0.6);
                var seq = cc.spawn(out,moveTo);
                this.items[i].runAction(seq);
            }else if( i == 2 ){
                var scale = cc.scaleTo(0.5,0.6);
                var seq = cc.spawn(scale,moveTo);
                this.items[i].runAction(seq);
            }else if(i == 3 ){
                var scale = cc.scaleTo(0.5,0.8);
                var seq = cc.spawn(scale,moveTo);
                this.items[i].runAction(seq);
            } else if( i == 4 ){
                var _in = cc.fadeIn(0.5);
                var seq = cc.spawn(_in,moveTo);
                this.items[i].runAction(seq);
            }
            else{
                this.items[i].runAction(moveTo);
            }
        }
    }




});
